import axios from "axios";
import type {
  AxiosError,
  AxiosRequestConfig,
  AxiosInstance,
  AxiosResponse,
  InternalAxiosRequestConfig,
} from "axios";

interface HYRequestInterceptors<T = AxiosResponse> {
  RequestSuccessFn?: (config: AxiosRequestConfig) => AxiosRequestConfig;
  RequsetFailureFn?: (err: AxiosError) => AxiosError;
  ResponseSuccessFn?: (res: T) => T;
  ResponseFailureFn?: (err: AxiosError) => AxiosError;
}
interface HYRequestConfig<T = AxiosResponse> extends AxiosRequestConfig {
  interceptors?: HYRequestInterceptors<T>;
}
//创建axios类
class HYRequest {
  //实例
  instance: AxiosInstance;
  interceptors?: HYRequestInterceptors;
  constructor(config: HYRequestConfig) {
    //创建axios实例
    this.instance = axios.create(config);
    // 全局拦截器start
    this.instance.interceptors.request.use(
      (config) => {
        // const token = getCurrentToken();
        // if (token && !config.headers?.Authorization) {
        //   config.headers.Authorization = `Bearer ${token}`;
        // }
        // console.log(config);
        return config;
      },
      (error: AxiosError) => {
        return Promise.reject(error);
      }
    );
    this.instance.interceptors.response.use(
      (res) => {
        // console.log(res);
        return res.data;
      },
      (error: AxiosError) => {
        // if (error.response?.status === 401) {
        //   setCurrentToken("");
        // }
        return error;
      }
    );
    //全局拦截器end

    //添加单个实例拦截start
    this.instance.interceptors.request.use(
      config.interceptors?.RequestSuccessFn as
        | ((config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig)
        | undefined,
      config.interceptors?.RequsetFailureFn
    );
    this.instance.interceptors.response.use(
      config.interceptors?.ResponseSuccessFn,
      config.interceptors?.ResponseFailureFn
    );
    //end
  }

  request<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return new Promise((resolve, reject) => {
      if (config.interceptors?.RequestSuccessFn) {
        config = config.interceptors.RequestSuccessFn(config);
      }
      this.instance
        .request<any, T>(config)
        .then((res) => {
          if (config.interceptors?.ResponseSuccessFn) {
            res = config.interceptors.ResponseSuccessFn(res);
          }
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  get<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "GET" });
  }

  post<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "POST" });
  }

  delete<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "DELETE" });
  }

  patch<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "PATCH" });
  }
  put<T = any>(config: HYRequestConfig<T>): Promise<T> {
    return this.request<T>({ ...config, method: "PUT" });
  }
}
export default HYRequest;
